package com.marry.shirodemo.controller;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author ml
 * @create 2017-11-03--14:28
 */
@Controller
public class HomeController {

    @RequestMapping({"/","/index"})
    public String index(){
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        return "index";
    }

    @GetMapping(value = "/login")
    public String login(){
        return "/login";
    }

    @PostMapping(value = "/login")
    public String login(HttpServletRequest request, Map<String,Object> map){

        System.out.println("HomeController.login");
        //登录失败从request获取到shiro处理的异常信息
        //shiroLoginFailure：shiro异常类的全类型

        String exception= (String) request.getAttribute("shiroLoginFailure");
        String msg="";
        if(exception!=null){
            if(UnknownAccountException.class.getName().equals(exception)){
                System.out.println("UnknownAccountException-->账号不存在");
                msg="UnknownAccountException-->账号不存在";
            } else if (IncorrectCredentialsException.class.getName().equals(exception)) {
                System.out.println("IncorrectCredentialsException-->密码不正确");
                msg="IncorrectCredentialsException-->密码不正确";

            }else if("kaptchaValidateFailed".equals(exception)){
                System.out.println("kaptchaValidateFailed-->验证码错误");
                msg="kaptchaValidateFailed-->验证码错误";
            }else{
                System.out.println("else -->"+exception);
                msg="else-->"+exception;
            }


        }
        //此方法不处理登录成功的情况 只处理失败的
        map.put("msg",msg);
        return "/login";

    }
}
